package cn.iocoder.yudao.module.worker.controller.app.clerkfans;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerk.ClerkDO;
import cn.iocoder.yudao.module.worker.dal.dataobject.trend.WorkerTrendDO;
import cn.iocoder.yudao.module.worker.service.clerk.ClerkService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.iocoder.yudao.module.worker.controller.app.clerkfans.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerkfans.ClerkFansDO;
import cn.iocoder.yudao.module.worker.service.clerkfans.ClerkFansService;

@Tag(name = "用户 APP - 店员关注")
@RestController
@RequestMapping("/worker/clerk-fans")
@Validated
public class AppClerkFansController {

    @Resource
    private ClerkFansService clerkFansService;

    @Resource
    private ClerkService clerkService;

    @PostMapping("/create")
    @Operation(summary = "创建店员关注")
    @PreAuthenticated
    public CommonResult<Long> createClerkFans(@Valid @RequestBody AppClerkFansSaveReqVO createReqVO) {
        createReqVO.setUserId(getLoginUserId());
        return success(clerkFansService.createClerkFans(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店员关注")
    public CommonResult<Boolean> updateClerkFans(@Valid @RequestBody AppClerkFansSaveReqVO updateReqVO) {
        clerkFansService.updateClerkFans(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店员关注")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteClerkFans(@RequestParam("id") Long id) {
        clerkFansService.deleteClerkFans(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员关注")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppClerkFansRespVO> getClerkFans(@RequestParam("id") Long id) {
        ClerkFansDO clerkFans = clerkFansService.getClerkFans(id);
        return success(BeanUtils.toBean(clerkFans, AppClerkFansRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员关注分页")
    @PreAuthenticated
    public CommonResult<PageResult<AppClerkFansRespVO>> getClerkFansPage(@Valid AppClerkFansPageReqVO pageReqVO) {
        pageReqVO.setUserId(getLoginUserId());
        PageResult<ClerkFansDO> pageResult = clerkFansService.getClerkFansPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty());
        }

        List<ClerkDO> clerkList = clerkService.getClerkList(convertSet(pageResult.getList(), ClerkFansDO::getWorkerClerkId));
        Map<Long, ClerkDO> mapClerk = convertMap(clerkList, ClerkDO::getId);
        return success(BeanUtils.toBean(pageResult, AppClerkFansRespVO.class, e -> e
                .setFans(true)
                .setAvatar(mapClerk.getOrDefault(e.getWorkerClerkId(), new ClerkDO()).getAvatar())
                .setSex(mapClerk.getOrDefault(e.getWorkerClerkId(), new ClerkDO()).getSex())
                .setNickname(mapClerk.getOrDefault(e.getWorkerClerkId(), new ClerkDO()).getNickname())
                .setIntro(mapClerk.getOrDefault(e.getWorkerClerkId(), new ClerkDO()).getIntro())
                .setCity(mapClerk.getOrDefault(e.getWorkerClerkId(), new ClerkDO()).getCity())
                .setOnlineStatus(mapClerk.getOrDefault(e.getWorkerClerkId(), new ClerkDO()).getOnlineStatus())
                .setFansNum(mapClerk.getOrDefault(e.getWorkerClerkId(), new ClerkDO()).getFans())
                .setAge(mapClerk.getOrDefault(e.getWorkerClerkId(), new ClerkDO()).getAge())));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店员关注 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClerkFansExcel(@Valid AppClerkFansPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClerkFansDO> list = clerkFansService.getClerkFansPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店员关注.xls", "数据", AppClerkFansRespVO.class,
                        BeanUtils.toBean(list, AppClerkFansRespVO.class));
    }

}